package com.wei.shopping.controller;

import com.wei.shopping.entrty.PageResult;
import com.wei.shopping.entrty.QueryPageBean;
import com.wei.shopping.entrty.Result;
import com.wei.shopping.pojo.News;
import com.wei.shopping.service.NewsService;
import com.wei.shopping.utils.KeyUtil;
import com.wei.shopping.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

/**
 * 公告
 */
@Controller
@RequestMapping("/news")
public class NewsController {
    @Resource
    private NewsService newsService;

    @Value("${qiniuyun}")
    private String qiniuyun;
    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        //获取文件的原始文件明
        String originalFilename = imgFile.getOriginalFilename();
        //获取文件的后缀名
        int last = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(last);
        //获取随机的文件名称
        String filename = UUID.randomUUID().toString()+ substring;
        //上传文件到服务器
        try{
            QiniuUtils.upload2Qinui(imgFile.getBytes(), filename);
            Result result = new Result(true,"图片上传成功");
            System.out.println(qiniuyun+"----------------");
            result.setData(qiniuyun + filename);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, "图片上传失败");
        }
    }
    /**
     * 提交请求
     */
    @ResponseBody
    @RequestMapping("/submit")
    public Result submit(@RequestBody News news, HttpServletRequest request){
        news.setId(KeyUtil.genUniqueKey());
        news.setUsername((String)request.getSession().getAttribute("username"));
        //插入
        try{
            news.setImage(news.getImage());
            newsService.add(news);
            return new Result(true,"添加成功");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }

    /**
     * 查找全部公告
     * @param queryPageBean
     * @return
     */
    @ResponseBody
    @RequestMapping("/findall")
    public PageResult findall(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult page = newsService.findall(queryPageBean);
            return page;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查看公告详情
     * @param id
     * @param modelMap
     * @param session
     * @return
     */
    @RequestMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap modelMap, HttpSession session){
        //浏览量+1
        newsService.addNewsRednumber(id);
        News news = newsService.findById(id);
        if (StringUtils.isEmpty(news)){
            return "/error/404";
        }
        modelMap.put("news",news);
        return "/news/detail";
    }

    /**
     * 更新数据
     * @param news
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result update(@RequestBody News news){
        try{
            newsService.update(news);
            return new Result(true,"更新成功");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"更新失败");
        }
    }

    /**
     * 删除公告
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable("id") String id){
        try{
            newsService.delete(id);
            return new Result(true,"删除成功");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
}
